/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.nii.qlks.GUI;

import edu.nii.qlks.ControlLayer.LoaiKhachHangControl;
import edu.nii.qlks.DataTransferObject.LoaiKhachHangInfo;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author 10520_000
 */
public class jfrm_ThemLoaiKhachHang extends javax.swing.JInternalFrame {

    /**
     * Creates new form jfrm_ThemLoaiKhachHang
     */
    LoaiKhachHangControl loaikhCT;
    LoaiKhachHangInfo loaikhInfo;

    public jfrm_ThemLoaiKhachHang() {
        initComponents();
        loaikhCT = new LoaiKhachHangControl();
        loaikhInfo = new LoaiKhachHangInfo();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jTextField_TenLoaiKhachHang = new javax.swing.JTextField();
        jTextField_HeSoLoaiKhachHang = new javax.swing.JTextField();
        jXLabel2 = new org.jdesktop.swingx.JXLabel();
        jXLabel3 = new org.jdesktop.swingx.JXLabel();
        jButton_LamMoi = new javax.swing.JButton();
        jButton_Them = new javax.swing.JButton();
        jButton_Thoat = new javax.swing.JButton();
        jXLabel1 = new org.jdesktop.swingx.JXLabel();

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin loại khách hàng", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

        jTextField_TenLoaiKhachHang.setToolTipText("Nhập tên loại khách hàng");

        jTextField_HeSoLoaiKhachHang.setToolTipText("Nhập hệ số loại khách hàng");
        jTextField_HeSoLoaiKhachHang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField_HeSoLoaiKhachHangActionPerformed(evt);
            }
        });

        jXLabel2.setText("Tên Loại Khách Hàng");
        jXLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jXLabel3.setText("Hệ Số Loại Khách Hàng");
        jXLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(90, 90, 90)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(42, 42, 42)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextField_TenLoaiKhachHang)
                    .addComponent(jTextField_HeSoLoaiKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(114, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(56, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField_TenLoaiKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jXLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField_HeSoLoaiKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(62, 62, 62))
        );

        jButton_LamMoi.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton_LamMoi.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/refresh.png"))); // NOI18N
        jButton_LamMoi.setText("Làm mới");
        jButton_LamMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_LamMoiActionPerformed(evt);
            }
        });

        jButton_Them.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton_Them.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/add.png"))); // NOI18N
        jButton_Them.setText("Thêm");
        jButton_Them.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ThemActionPerformed(evt);
            }
        });

        jButton_Thoat.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton_Thoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit_16.png"))); // NOI18N
        jButton_Thoat.setText("Đóng");
        jButton_Thoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ThoatActionPerformed(evt);
            }
        });

        jXLabel1.setText("THÊM LOẠI KHÁCH HÀNG");
        jXLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(243, 243, 243)
                .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jButton_LamMoi)
                        .addGap(41, 41, 41)
                        .addComponent(jButton_Them, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(jButton_Thoat, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(71, 71, 71))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_LamMoi, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton_Them, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton_Thoat, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(47, 47, 47))
        );

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_LamMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_LamMoiActionPerformed
        // TODO add your handling code here:
        jTextField_TenLoaiKhachHang.setEnabled(true);
        jTextField_HeSoLoaiKhachHang.setEnabled(true);
        jTextField_HeSoLoaiKhachHang.setText("");
        jTextField_TenLoaiKhachHang.setText("");
        jButton_Them.setEnabled(true);
    }//GEN-LAST:event_jButton_LamMoiActionPerformed

    private void jButton_ThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ThemActionPerformed
        // TODO add your handling code here:
        if (jTextField_TenLoaiKhachHang.getText().equals("") && jTextField_HeSoLoaiKhachHang.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Tên loại khách hàng & Hệ số loại khách hàng không được để trống!");
        } else if (jTextField_TenLoaiKhachHang.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Tên loại khách hàng không được để trống!");
        } else if (jTextField_HeSoLoaiKhachHang.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Hệ số loại khách hàng không được để trống!");
        } else {

            loaikhInfo.setTenLoaiKhach(jTextField_TenLoaiKhachHang.getText());
            try {
                loaikhInfo.setHeSoLoaiKhach(Float.parseFloat(jTextField_HeSoLoaiKhachHang.getText()));

                try {

                    Vector vt = loaikhCT.LayDS();
                    if (vt.size() > 0) 
                    {
                        for (int i = 0; i < vt.size(); i++) 
                        {
                            Vector vt_row = (Vector) vt.get(i);
                            String TenLoaiKhach = vt_row.get(1).toString().trim();
                            if (TenLoaiKhach.equalsIgnoreCase(jTextField_TenLoaiKhachHang.getText().trim())) 
                            {
                                JOptionPane.showMessageDialog(this, "Tên loại khách đã tồn tại!", "Lỗi", JOptionPane.ERROR_MESSAGE);
                                jTextField_TenLoaiKhachHang.requestFocus();
                                jTextField_TenLoaiKhachHang.setText("");
                                jTextField_HeSoLoaiKhachHang.setText("");
                                return;

                            }
                        }
                        Vector vt_row = (Vector) vt.get(vt.size() - 1);
                        String ma = vt_row.get(0).toString().trim();
                        loaikhInfo.setMaLoaiKhach(loaikhCT.TaoMaTuDong(ma, 3));
                    } else {
                        loaikhInfo.setMaLoaiKhach(loaikhCT.TaoMaTuDong("MLK000", 3));
                    }

                    loaikhCT.Insert(loaikhInfo);

                    jButton_Them.setEnabled(false);
                    jTextField_TenLoaiKhachHang.setEnabled(false);
                    jTextField_HeSoLoaiKhachHang.setEnabled(false);

                    JOptionPane.showMessageDialog(this, "Thêm loại khách hàng thành công!", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                } catch (Exception ex) {
                    Logger.getLogger(jfrm_ThemLoaiKhachHang.class.getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(this, "Thêm loại khách hàng thất bại!", "Lỗi", JOptionPane.ERROR_MESSAGE);
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Hệ số loại khách hàng chỉ được nhập số!", "Lỗi", JOptionPane.ERROR_MESSAGE);
                jTextField_HeSoLoaiKhachHang.setText("");
                jTextField_HeSoLoaiKhachHang.requestFocus();
                return;
            }
        }

    }//GEN-LAST:event_jButton_ThemActionPerformed

    private void jButton_ThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton_ThoatActionPerformed

    private void jTextField_HeSoLoaiKhachHangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField_HeSoLoaiKhachHangActionPerformed
        jButton_ThemActionPerformed(evt);
    }//GEN-LAST:event_jTextField_HeSoLoaiKhachHangActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_LamMoi;
    private javax.swing.JButton jButton_Them;
    private javax.swing.JButton jButton_Thoat;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField_HeSoLoaiKhachHang;
    private javax.swing.JTextField jTextField_TenLoaiKhachHang;
    private org.jdesktop.swingx.JXLabel jXLabel1;
    private org.jdesktop.swingx.JXLabel jXLabel2;
    private org.jdesktop.swingx.JXLabel jXLabel3;
    // End of variables declaration//GEN-END:variables
}
